home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / System / DragX 1.0a1 / Source / DragX.cp next >
Text File  |  1996-05-02  |  7KB  |  342 lines

  1. /*
  2.  *        DragX.cp
  3.  *
  4.  *        Copyright © 1995-96 Marco Piovanelli <mailto:piovanel@kagi.com>
  5.  *        All Rights Reversed
  6.  */
  7.  
  8. #include "DragX.h"
  9.  
  10. pascal ComponentResult main
  11.     (
  12.         ComponentParameters * const params ,
  13.         const Handle globals
  14.     )
  15. {
  16.     ProcPtr proc = nil ;
  17.     ComponentResult result = 0 ;
  18.     SInt8 saveState ;
  19.  
  20.     switch ( params -> what )
  21.     {
  22.  
  23.         // Common Component Manager selectors
  24.  
  25.         case kComponentVersionSelect :
  26.             result = kDragXVersion ;
  27.             break ;
  28.  
  29.         case kComponentCanDoSelect :
  30.             proc = ( ProcPtr ) DragX_CanDo ;
  31.             break ;
  32.  
  33.         case kComponentOpenSelect :
  34.             proc = ( ProcPtr ) DragX_Open ;
  35.             break ;
  36.  
  37.         case kComponentCloseSelect :
  38.             proc = ( ProcPtr ) DragX_Close ;
  39.             break ;
  40.  
  41.         //        selectors for Translation Extensions
  42.  
  43.         case kTranslateGetScrapTranslationList :
  44.             proc = ( ProcPtr ) DragX_GetScrapTranslationList ;
  45.             break ;
  46.  
  47.         case kTranslateIdentifyScrap :
  48.             proc = ( ProcPtr ) DragX_IdentifyScrap ;
  49.             break ;
  50.  
  51.         case kTranslateTranslateScrap :
  52.             proc = ( ProcPtr ) DragX_TranslateScrap ;
  53.             break;
  54.  
  55.         default :
  56.             result = badComponentSelector ;
  57.             break ;
  58.     }
  59.  
  60.     if ( proc != nil )
  61.     {
  62.         if ( globals != nil )
  63.         {
  64.             saveState = HGetState ( globals ) ;
  65.             HLock ( globals ) ;
  66.         }
  67.  
  68.         result = CallComponentFunctionWithStorage ( ( globals == nil ) ? nil : ( Handle ) * globals, params, proc ) ;
  69.  
  70.         if ( globals != nil )
  71.         {
  72.             HSetState ( globals, saveState ) ;
  73.         }
  74.     }
  75.  
  76.     return result;
  77. }
  78.  
  79. pascal ComponentResult DragX_CanDo ( GlobalsPtrConst g, const SInt16 selector )
  80. {
  81. #pragma unused ( g )
  82.  
  83.     if     (    ( ( selector >= kComponentVersionSelect ) && ( selector <=kComponentOpenSelect ) ) ||
  84.             ( ( selector >= kTranslateGetScrapTranslationList ) && ( selector <= kTranslateTranslateScrap ) ) )
  85.     {
  86.         return ( ComponentResult ) true ;
  87.     }
  88.     else
  89.     {
  90.         return ( ComponentResult ) false ;
  91.     }
  92. }
  93.  
  94. pascal ComponentResult DragX_Open
  95.     (
  96.         GlobalsPtr g ,
  97.         const ComponentInstance self
  98.     )
  99. {
  100.     OSErr err ;
  101.     Handle storage ;
  102.  
  103.     // allocate the storage handle
  104.     storage = NewHandleClear ( sizeof ( struct GlobalsRecord ) ) ;
  105.     if ( ( err = MemError ( ) ) != noErr )
  106.     {
  107.         return err ;
  108.     }
  109.  
  110.     g = ( GlobalsPtr ) ( * storage ) ;
  111.     g -> signature = kDragXSignature ;
  112.     g -> instance = self ;
  113.     SetComponentInstanceStorage ( self, storage ) ;
  114.  
  115.     return noErr;
  116. }
  117.  
  118. pascal ComponentResult DragX_Close
  119.     (
  120.         GlobalsPtrConst g ,
  121.         const ComponentInstance self
  122.     )
  123. {
  124. #pragma unused ( g )
  125.  
  126.     Handle storage = GetComponentInstanceStorage ( self ) ;
  127.  
  128.     //        dispose of the instance storage
  129.     if ( storage != nil )
  130.     {
  131.         DisposeHandle ( storage ) ;
  132.         SetComponentInstanceStorage ( self, nil ) ;
  133.     }
  134.  
  135.     return noErr;
  136. }
  137.  
  138. pascal ComponentResult DragX_GetScrapTranslationList
  139.     (
  140.         GlobalsPtrConst g ,
  141.         const Handle list
  142.     )
  143. {
  144. #pragma unused ( g )
  145.  
  146.     struct DragXScrapTranslationList * pl ;
  147.     OSErr err ;
  148.  
  149.     // do nothing if the scrap translation list has already been initialized
  150.     pl = ( struct DragXScrapTranslationList * ) ( * list ) ;
  151.     if ( pl -> header . modDate == kDragXSignature )
  152.     {
  153.         return noErr ;
  154.     }
  155.  
  156.     // resize the translation list
  157.     SetHandleSize ( list, sizeof ( struct DragXScrapTranslationList ) ) ;
  158.     if ( ( err = MemError ( ) ) != noErr )
  159.     {
  160.         return err ;
  161.     }
  162.  
  163.     // fill it in
  164.     pl = ( struct DragXScrapTranslationList * ) ( * list ) ;
  165.     BlockClear ( pl, sizeof ( * pl ) ) ;
  166.     pl -> header . modDate = kDragXSignature ;
  167.     pl -> header . groupCount = 1 ;
  168.     pl -> srcCount = 1 ;
  169.     pl -> srcEntrySize = sizeof ( ScrapTypeSpec ) ;
  170.     pl -> srcSpec [ 0 ] . format = flavorTypeHFS ;
  171.     pl -> dstCount = 1 ;
  172.     pl -> dstEntrySize = sizeof ( ScrapTypeSpec ) ;
  173.     pl -> dstSpec [ 0 ] . format = flavorTypeStyledText ;
  174.  
  175.     return noErr ;
  176. }
  177.  
  178. pascal ComponentResult DragX_IdentifyScrap
  179.     (
  180.         GlobalsPtrConst g ,
  181.         const void * const dataPtr ,
  182.         const Size dataSize ,
  183.         ScrapType * const dataFormat
  184.     )
  185. {
  186. #pragma unused ( g, dataFormat )
  187.  
  188.     const HFSFlavor * const fl = ( const HFSFlavor * ) dataPtr ;
  189.  
  190.     if ( ( dataSize < kMinHFSFlavorSize ) || ( fl -> fileType != flavorTypeText ) )
  191.     {
  192.         //* dataFormat = '\?\?\?\?' ;
  193.         return noTypeErr ;
  194.     }
  195.     
  196.     return noErr ;
  197. }
  198.  
  199. pascal ComponentResult DragX_TranslateScrap
  200.     (
  201.         GlobalsPtrConst g ,
  202.         const TranslationRefNum refNum ,
  203.         const void * const srcDataPtr ,
  204.         Size srcDataSize ,
  205.         const ScrapType srcType ,
  206.         const SInt32 srcTypeHint ,
  207.         Handle dstData ,
  208.         const ScrapType dstType ,
  209.         const SInt32 dstTypeHint
  210.     )
  211. {
  212. #pragma unused ( g, refNum, srcTypeHint, dstTypeHint )
  213.  
  214.     const HFSFlavor * const fl = ( const HFSFlavor * ) srcDataPtr ;
  215.     short dataForkRefNum = 0 ;
  216.     short resForkRefNum ;
  217.     Handle hStyles = nil ;
  218.     Size stylesSize ;
  219.     Size textSize ;
  220.     ParamBlockRec pb ;
  221.     OSErr err ;
  222.  
  223.     // make sure source data type is HFS
  224.     if ( srcType != flavorTypeHFS )
  225.     {
  226.         goto cleanup ;
  227.     }
  228.  
  229.     // make sure requested flavor is styled text
  230.     if ( dstType != flavorTypeStyledText )
  231.     {
  232.         goto cleanup ;
  233.     }
  234.  
  235.     //    make sure source data is long enough to be valid
  236.     if ( srcDataSize < kMinHFSFlavorSize )
  237.     {
  238.         goto cleanup ;
  239.     }
  240.  
  241.     // is this a text file?
  242.     err = badDragFlavorErr;
  243.     if ( fl -> fileType != flavorTypeText )
  244.     {
  245.         goto cleanup ;
  246.     }
  247.  
  248.     // try to open the resource fork
  249.     if ( ( resForkRefNum = FSpOpenResFile ( & fl -> fileSpec, fsRdPerm ) ) != -1 )
  250.     {
  251.  
  252.         // get first 'styl' resource
  253.         if ( ( hStyles = Get1IndResource ( flavorTypeTextStyles, 1 ) ) != nil )
  254.         {
  255.             DetachResource ( hStyles ) ;
  256.         }
  257.  
  258.         //    close resource file
  259.         CloseResFile ( resForkRefNum ) ;
  260.     }
  261.  
  262.     //    if no 'styl' resource was found, fabricate one
  263.     if ( hStyles == nil )
  264.     {
  265.         hStyles = NewHandleClear ( sizeof ( short ) + sizeof ( ScrpSTElement ) ) ;
  266.         if ( ( err = MemError ( ) ) != noErr )
  267.         {
  268.             goto cleanup ;
  269.         }
  270.         StScrpRec * const pStyles = * ( StScrpHandle ) hStyles ;
  271.         pStyles -> scrpNStyles = 1 ;
  272.         pStyles -> scrpStyleTab [ 0 ] . scrpFont = applFont ;
  273.     }
  274.  
  275.     //        copy the styles to the destination handle
  276.     stylesSize = InlineGetHandleSize ( hStyles ) ;
  277.     SetHandleSize ( dstData, stylesSize ) ;
  278.     if ( ( err = MemError ( ) ) != noErr )
  279.     {
  280.         goto cleanup ;
  281.     }
  282.     BlockMoveData ( * hStyles, * dstData, stylesSize ) ;
  283.     DisposeHandle ( hStyles ) ;
  284.     hStyles = nil ;
  285.  
  286.     //        open the data fork
  287.     if ( ( err = FSpOpenDF ( & fl -> fileSpec, fsRdPerm, & dataForkRefNum ) ) != noErr )
  288.     {
  289.         goto cleanup ;
  290.     }
  291.  
  292.     //        get text size
  293.     BlockClear ( & pb, sizeof ( pb ) ) ;
  294.     pb . ioParam . ioRefNum = dataForkRefNum ;
  295.     if ( ( err = PBGetEOFSync ( & pb) ) != noErr )
  296.     {
  297.         goto cleanup ;
  298.     }
  299.     textSize = ( Size ) pb . ioParam . ioMisc ;
  300.  
  301.     // resize destination data handle
  302.     SetHandleSize ( dstData, stylesSize + textSize ) ;
  303.     if ( ( err = MemError ( ) ) != noErr )
  304.     {
  305.         goto cleanup ;
  306.     }
  307.  
  308.     //    read text
  309.     BlockClear ( & pb, sizeof ( pb ) ) ;
  310.     pb . ioParam . ioRefNum = dataForkRefNum ;
  311.     pb . ioParam . ioBuffer = * dstData + stylesSize ;
  312.     pb . ioParam . ioReqCount = textSize ;
  313.     pb . ioParam . ioPosMode = fsFromStart ;
  314.     if ( ( err = PBReadSync( &pb) ) != noErr )
  315.     {
  316.         goto cleanup ;
  317.     }
  318.  
  319.     // clear result code
  320.     err = noErr ;
  321.  
  322. cleanup:
  323.     if ( dataForkRefNum )
  324.     {
  325.         BlockClear ( & pb, sizeof ( pb ) ) ;
  326.         pb . ioParam . ioRefNum = dataForkRefNum ;
  327.         PBCloseSync ( & pb ) ;
  328.     }
  329.  
  330.     return err ;
  331. }
  332.  
  333. void BlockClear ( void * blockPtr, Size blockSize )
  334. {
  335.     register char * p = ( char * ) blockPtr ;
  336.  
  337.     while ( --blockSize >= 0 )
  338.     {
  339.         * p ++ = 0 ;
  340.     }
  341. }
  342.